import java.io.FileOutputStream;
import java.util.Hashtable;

import org.htmlparser.Node;
import org.htmlparser.Parser;
import org.htmlparser.Tag;
import org.htmlparser.filters.HasAttributeFilter;
import org.htmlparser.filters.OrFilter;
import org.htmlparser.util.NodeList;
import org.htmlparser.util.ParserException;

import sun.net.TelnetInputStream;
import sun.net.ftp.FtpClient;

import com.sun.net.*;

public class HtmlParser
{

        public HtmlParser()
        {
                
        }
        
        public NodeList getBoletimList()
        {
                Parser parser;
                
                HasAttributeFilter filterClassImpar = new HasAttributeFilter();
                HasAttributeFilter filterClassPar = new HasAttributeFilter();
                
                filterClassImpar.setAttributeName("class");
                filterClassImpar.setAttributeValue("impar");
                
                filterClassPar.setAttributeName("class");
                filterClassPar.setAttributeValue("par");
                
                OrFilter orfilter = new OrFilter(filterClassImpar,filterClassPar);
                NodeList list = null;
                
                try
                {
                        parser = new Parser("http://transito.rio.rj.gov.br/entrada.cfm?pagina=6&menusel=5");
                        list = parser.parse(orfilter);
                        
                        if(list.size() < 1)
                                return null;
                } 
                catch (ParserException e) { e.printStackTrace(); }
                
                return list;
        }
        
        public Hashtable<String, String> extractData(NodeList list)
        {
                Hashtable< String, String> hash = new Hashtable< String, String>(); 
                
                for(int i = 0; i < list.size(); i++)
                {
                        NodeList tds = list.elementAt(i).getChildren();

                        if(tds.size() < 4)
                                return null;
                        
                        Node tdstreet = tds.elementAt(1);
                        Node tdimg = tds.elementAt(3);
                        
                        if(tdimg.getChildren().size() < 2)
                                return null;
                        
                        Tag img = (Tag) tdimg.getChildren().elementAt(1);
                        
                        String fields[] = img.getAttribute("src").split("[./]");
                        
                        if(fields.length < 3)
                                return null;
                        
                        String imgvalue = fields[1];
                        String color = imgvalue.replaceAll("leg","").replaceAll("Maior", "").toLowerCase();
                        String streetname = tdstreet.toPlainTextString().trim().toLowerCase();
                
                        hash.put(streetname, color);
                }
                
                return hash;
        }
        
        public static void main(String[] args)
        {
                Parser parser;
                
                HasAttributeFilter filterClassImpar = new HasAttributeFilter();
                HasAttributeFilter filterClassPar = new HasAttributeFilter();
                
                filterClassImpar.setAttributeName("class");
                filterClassImpar.setAttributeValue("impar");
                
                filterClassPar.setAttributeName("class");
                filterClassPar.setAttributeValue("par");
                
                OrFilter orfilter = new OrFilter(filterClassImpar,filterClassPar);

                try
                {
                        parser = new Parser("http://transito.rio.rj.gov.br/entrada.cfm?pagina=6&menusel=5");
                        NodeList list = parser.parse(orfilter);
                        
                        for(int i = 0; i < list.size(); i++)
                        {
                                NodeList tds = list.elementAt(i).getChildren();

                                Node tdstreet = tds.elementAt(1);
                                Node tdimg = tds.elementAt(3);
                                Tag img = (Tag) tdimg.getChildren().elementAt(1);
                                String imgvalue = img.getAttribute("src").split("[./]")[1];
                                String color = imgvalue.replaceAll("leg","").replaceAll("Maior", "").toLowerCase();
                        
                                System.out.println(tdstreet.toPlainTextString().trim() + " - " + color);
                                
                        }
                
                } 
                catch (ParserException e) { e.printStackTrace(); }
               
        }       
}